# write by hasaki
# first edit on 2019/01/17
# last change on 2019/04/16
# 这是个tick的策略
from datetime import datetime

from hashakisea.strategy.haStrategyTemplate import HaStrategy

class TickStrategy(HaStrategy):
    def __init__(self):     
        super(TickStrategy,self).__init__()
        self.strategy_name='tick strategy'
        self.pos=0         # 持仓
        self.cash=0     # 现金
        self.capital=0                   # 总资产
        self.side=0                 # 判断方向

    
    def next(self,data):
        '''
        tick数据：
        {"data": {"quoteVolume": "1141795.9880448482", "baseVolume": "2739104.51651617209508", 
        "highestBid": "2.3948", "high24hr": "2.435", "last": "2.3948", "lowestAsk": "2.3959", "elapsed":
         "10ms", "result": "true", "low24hr": "2.3563", "percentChange": "1.5821845174974", "target_symbol": "eos_usdt"}, "type": "tick"}
        '''
        target_symbol=data['target_symbol']
        percent=float(data['percentChange'])
        ask_price=float(data['lowestAsk'])
        bid_price=float(data['highestBid'])
        volume=10
        if percent>1:
            if self.side==-1:
                return

            self.sell(target_symbol,bid_price,volume)
            self.side-=1

        elif percent<-3:
            if self.side==1:
                return

            self.buy(target_symbol,ask_price,volume)
            self.side+=1
    
    def onOrder(self):
        print("{}策略已下单".format(self.strategy_name))
        time=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print('时间 ：{}   总资产：{} 初始资金：3000  持有现金：{}   持仓：{}'.format(time,self.capital,self.cash,self.pos))

    
    def onTrade(self):
        pass